﻿using System.Diagnostics;
using System.Runtime.InteropServices;
using UtilZ.Dotnet.Ex.Attributes;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.WindowsDesktopEx.Base;
using UtilZ.Dotnet.WindowsDesktopEx.Winform;

namespace WinFormApp
{
    public partial class FTestEmbedAppProcess : Form
    {
        public FTestEmbedAppProcess()
        {
            InitializeComponent();
        }

        private enum MainWindowQueryType
        {
            /// <summary>
            /// 类名
            /// </summary>
            [DisplayNameEx("类名")]
            ClassName,

            /// <summary>
            /// 标题
            /// </summary>
            [DisplayNameEx("标题")]
            Caption
        }

        private void FTestEmbedAppProcess_Load(object sender, EventArgs e)
        {
            DropdownBoxHelper.BindingEnumToComboBox<MainWindowQueryType>(comboBoxQueryType, MainWindowQueryType.Caption);
        }


        private WinformAppEmbedder _colorEmbedApp = null;
        private void btnColor_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex != 0)
            {
                return;
            }

            if (this._colorEmbedApp != null)
            {
                this._colorEmbedApp.Dispose();
                this._colorEmbedApp = null;
                return;
            }

            Panel panel;
            if (tabControl1.SelectedTab.Controls.Count > 0)
            {
                panel = (Panel)tabControl1.SelectedTab.Controls[0];
            }
            else
            {
                panel = new Panel();
                panel.Dock = DockStyle.Fill;
                tabControl1.SelectedTab.Controls.Add(panel);
            }

            this._colorEmbedApp = new WinformAppEmbedder(panel, @"E:\Soft\Color\Color.exe", false);
            this._colorEmbedApp.EmbedWindowOffset = new Point(100, -50);
            this._colorEmbedApp.Start();
        }

        private void btnDetach_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex != 0)
            {
                return;
            }

            if (this._colorEmbedApp != null)
            {
                this._colorEmbedApp.Detach();
            }
        }

        private void btnAttach_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex != 0)
            {
                return;
            }

            if (this._colorEmbedApp != null)
            {
                this._colorEmbedApp.Attach();
            }
        }

        private WinformAppEmbedder _netAssistEmbedApp = null;
        private void btnNetAssist_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex != 1)
            {
                return;
            }

            if (this._netAssistEmbedApp != null)
            {
                return;
            }

            Panel panel = new Panel();
            panel.Dock = DockStyle.Fill;
            tabControl1.SelectedTab.Controls.Add(panel);
            this._netAssistEmbedApp = new WinformAppEmbedder(panel, @"E:\Soft\NetAssist.exe", true);
            this._netAssistEmbedApp.MainWindowName = "网络调试助手";
            this._netAssistEmbedApp.Filter += _netAssistEmbedApp_Filter;
            this._netAssistEmbedApp.Start();
        }

        private void _netAssistEmbedApp_Filter(object sender, MainWindowHandleResolveEventArgs e)
        {
            //网络调试助手第一个句柄就可以,第二个就不行
            e.MainWindowHandle = e.WindowHandleNames[0].Value;
            //e.MainWindowHandle = e.WindowHandleNames[1].Value;
        }

        private void FTestEmbedAppProcess_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this._colorEmbedApp != null)
            {
                this._colorEmbedApp.Dispose();
                this._colorEmbedApp = null;
            }


            if (this._netAssistEmbedApp != null)
            {
                this._netAssistEmbedApp.Dispose();
                this._netAssistEmbedApp = null;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            IntPtr handle;
            var mainWindowQueryType = DropdownBoxHelper.GetEnumFromComboBox<MainWindowQueryType>(comboBoxQueryType);

            if (mainWindowQueryType == MainWindowQueryType.ClassName)
            {
                handle = WindowHelper.FindWindow(txtMainWindowName.Text, null);
            }
            else
            {
                handle = WindowHelper.FindWindow(null, txtMainWindowName.Text);
            }

            txtMainWindowHandle.Text = ConvertEx.ToHexadecimaString((long)handle);
            //var hadle1 = WindowHelper.FindWindow("notepad++", null);
            //var hadle2 = WindowHelper.FindWindow(null, @"*D:\tmp.txt - Notepad++");
        }

        private Process _process = null;
        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                if (this._process == null)
                {
                    this._process = Process.GetProcessesByName("Color").FirstOrDefault();
                    this._process.StartInfo = new ProcessStartInfo()
                    {
                        FileName = this._process.MainModule.FileName,
                        UseShellExecute = true,
                    };
                    return;

                    //this._process = new Process()
                    //{
                    //    StartInfo = new ProcessStartInfo()
                    //    {
                    //        FileName = @"E:\Soft\Color\Color.exe",
                    //        UseShellExecute = true,
                    //    }
                    //};
                }

                this._process.Start();
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }


    }
}
